import 'package:flutter/widgets.dart';
import 'package:get/get.dart';
import 'package:xmshop/app/services/httpsClient.dart';
import 'package:xmshop/app/services/storage.dart';
import '../../../../models/message_model.dart';
import '../../../../models/user_model.dart';

class RegisterStepThreeController extends GetxController {
  final TextEditingController passController = TextEditingController();
  final TextEditingController confirmPassController = TextEditingController();
  HttpsClient httpsClient = HttpsClient();
  String tel = Get.arguments["tel"];
  String code = Get.arguments["code"];

  // 注册
  Future<MessageModel> doRegister() async {
    var response = await httpsClient.post("/api/register",
        data: {"tel": tel, "code": code, "password": passController.text});

    if (response != null) {
      if (response.data["success"]) {
        // 保存用户信息到本地
        Storage.setData(
            "userInfo", UserModel.fromJson(response.data["userinfo"][0]));

        return MessageModel(success: true, message: "注册成功");
      }
      return MessageModel(success: false, message: response.data["message"]);
    }

    return MessageModel(success: false, message: "网络异常");
  }
}
